package com.sea.physical.controller;

import com.codecrane.core.controller.UploaderController;
import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.sea.physical.entity.CreatePackingSku;
import com.sea.physical.entity.CreatePlan;
import com.sea.physical.service.*;
import com.sea.supply.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.List;

/**
 * 创建发运计划明细
 * @author 陈鸿飞
 */
@Controller
@RequestMapping(value = "/sea/physical/createPackingSku")
public class CreatePackingSkuController {

    @Autowired
    private CreatePackingSkuService createPackingSkuService;
    @Autowired
    private DictDetailService dictDetailService;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private CreatePlanService createPlanService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private TransportService transportService;

    /**
     * 新增页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        getModel(model);
        return "/sea/physical/createPackingSku/main";
    }

    void getModel(Model model){
        model.addAttribute("cabinet",cabinetService.findByCnd(null));
        model.addAttribute("transport",transportService.findByCnd( null));
        model.addAttribute("country",dictDetailService.findByTypeCode("base_country", null));
        model.addAttribute("supplier",supplierService.findByCnd( null));
    }


    @RequestMapping("/import")
    @ResponseBody
    public AjaxReturn importSku(Model model,
                                MultipartFile importSession,
                                @RequestParam("createId")Integer createId) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try {
            if (null == importSession || importSession.isEmpty()) {
                return result.setMsg("请上传文件！！！");
            } else {
                String fileName = importSession.getOriginalFilename();
                if (!(fileName.contains(".xls"))) {
                    return result.setMsg("只能上传excel文件！！！");
                }
                InputStream is = importSession.getInputStream();
                createPackingSkuService.importSku(is,fileName,createId);

                UploaderController upload=new UploaderController();

                AjaxReturn ajaxReturn = upload.uploadFile(importSession, null);
                String string = ajaxReturn.getData().toString();
                createPlanService.updateCreateFile(string,createId);
                result.setOk(true);
                result.setMsg("上传成功！！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("导入异常，请联系管理员！");
            return result;
        }
        return result;
    }

    @RequestMapping("/findByCreateId")
    @ResponseBody
    public List<CreatePackingSku> findByCreateId(Integer createId){
        return createPackingSkuService.findDetailByCreateId(createId);
    }

}
